/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.applyjob.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;

/**
 * 职位申请Controller
 * @author bm
 * @version 2019-02-10
 */
@Controller
@RequestMapping(value = "${adminPath}/applyjob/bizJobApplay")
public class BizJobApplayController extends BaseController {

	@Autowired
	private BizJobApplayService bizJobApplayService;
	
	@ModelAttribute
	public BizJobApplay get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizJobApplay entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizJobApplayService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizJobApplay();
		}
		return entity;
	}
	
	@RequiresPermissions("applyjob:bizJobApplay:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizJobApplay bizJobApplay, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizJobApplay> page = bizJobApplayService.findPage(new Page<BizJobApplay>(request, response), bizJobApplay); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applyjob/bizJobApplayList";
		}
		return "biz/applyjob/bizJobApplayList";
	}
	
	@RequiresPermissions("applyjob:bizJobApplay:view")
	@RequestMapping(value = "form")
	public String form(BizJobApplay bizJobApplay, Model model) {
		model.addAttribute("bizJobApplay", bizJobApplay);
		return "biz/applyjob/bizJobApplayForm";
	}

	@RequiresPermissions("applyjob:bizJobApplay:edit")
	@RequestMapping(value = "save")
	public String save(BizJobApplay bizJobApplay, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizJobApplay)){
			return form(bizJobApplay, model);
		}
		try{
			bizJobApplayService.save(bizJobApplay);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applyjob/bizJobApplay?repage";
		}
		addMessage(redirectAttributes, "保存职位申请成功");
		return "redirect:"+Global.getAdminPath()+"/applyjob/bizJobApplay?repage";
	}
	
	@RequiresPermissions("applyjob:bizJobApplay:edit")
	@RequestMapping(value = "delete")
	public String delete(BizJobApplay bizJobApplay, RedirectAttributes redirectAttributes) {
		try{
			bizJobApplayService.delete(bizJobApplay);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applyjob/bizJobApplay?repage";
		}
		addMessage(redirectAttributes, "删除职位申请成功");
		return "redirect:"+Global.getAdminPath()+"/applyjob/bizJobApplay?repage";
	}

}